import java.util.InputMismatchException;
import java.util.Scanner;

public class NGuessNumber {
    public int[] generateAnswer(){//生成四个随机数，并存入数组返回
        int num1=0,num2=0,num3=0,num4=0;
        do {
            num1 = (int) (Math.random() * 10 + 0);
            num2 = (int) (Math.random() * 10 + 0);
            num3 = (int) (Math.random() * 10 + 0);
            num4 = (int) (Math.random() * 10 + 0);
        }while (num1==num2||num1==num3||num1==num4||num2==num3||num2==num4||num3==num4);
        int[] numarr = {num1,num2,num3,num4};
        //System.out.println(num1+" "+num2+" "+num3+" "+num4);
        return numarr;
    }

    public int[] getPlayerInput(){//接收用户输入的4个数字，并存入数组返回
        Scanner scanner  = new Scanner(System.in);
        int num1=0,num2=0,num3=0,num4=0;
        boolean flag;
        do {
            try {
                num1 = scanner.nextInt();
                num2 = scanner.nextInt();
                num3 = scanner.nextInt();
                num4 = scanner.nextInt();
                flag = false;
            }catch (InputMismatchException input){
                flag = true;
                scanner.nextLine();
                System.out.println("输入非法，请输入数字！");
            }
        }while (flag);

        int numarr[] = {num1,num2,num3,num4};
        return numarr;
    }

    public int compareGuessAnswer(int[] numarr1,int[] numarr2){//（答案，用户猜测）
        int a=0,b=0;
        for (int i=0;i<4;i++){
            for (int j=0;j<4;j++){
                if (numarr1[i] == numarr2[j]){
                    if (i==j){
                        a++;
                    }else {
                        b++;
                    }
                }
            }
        }
        System.out.println("提示："+a+"A"+b+"B");
        return a;
    }


    public boolean isWin(int a){//判断是否胜利,使用方法：isWin(nGuessNumber.compareGuessAnswer(nGuessNumber.generateAnswer(), nGuessNumber.getPlayerInput()))
        boolean flag;
        flag = 4==a;
        if (flag)
            System.out.println("猜测正确，游戏结束");
        return flag;
    }

    public static void main(String[] args) {
        boolean flag;
        String s;
        NGuessNumber nGuessNumber = new NGuessNumber();

        do {
            int[] arr = nGuessNumber.generateAnswer();
            do {
                flag = nGuessNumber.isWin(nGuessNumber.compareGuessAnswer(arr, nGuessNumber.getPlayerInput()));

            }while (false == flag);
            Scanner scanner = new Scanner(System.in);
            System.out.println("是否进行下一局游戏？Y/N");
            s = scanner.next();
        }while (s.equalsIgnoreCase("Y"));

    }
}
